package com.ibeeking.found.file.rest.service.office.services;

import com.ibeeking.found.file.rest.service.office.entities.GroupEntity;
import com.ibeeking.found.file.rest.service.office.repositories.GroupRepository;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

//@Service
public class GroupServices {

//    @Autowired
    private GroupRepository groupRepository;

    // create a new group with the specified name
    public GroupEntity createGroup(String name){
        if(name == null) return null;  // check if a name is specified
        Optional<GroupEntity> group = groupRepository.findGroupByName(name);  // check if group with such a name already exists
        if(group.isPresent()) return group.get();  // if it exists, return it
        GroupEntity newGroupEntity = new GroupEntity();
        newGroupEntity.setName(name);  // otherwise, create a new group with the specified name

//        groupRepository.save(newGroupEntity);  // save a new group

        return newGroupEntity;
    }

    // create a list of groups from the reviewGroups permission parameter
    public List<GroupEntity> createGroups(List<String> reviewGroups){
        if(reviewGroups == null) return null;  // check if the reviewGroups permission exists
        return reviewGroups.stream()  // convert this parameter to a list of groups whose changes the userEntity can accept/reject
                .map(group -> createGroup(group))
                .collect(Collectors.toList());
    }
}
